package liumengbaio.lolzhangyoubao.Activity.Activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;

import liumengbaio.lolzhangyoubao.Activity.Adapter.RealShowAdapter;
import liumengbaio.lolzhangyoubao.Activity.Adapter.RealShowGridViewAdapter;
import liumengbaio.lolzhangyoubao.Activity.InFo.RealShowItem;
import liumengbaio.lolzhangyoubao.Activity.Interface.RelectGetUrl;
import liumengbaio.lolzhangyoubao.R;

public class RealShowActivity extends AppCompatActivity implements View.OnClickListener, RelectGetUrl {
    private ListView listView;
    private GridView real_show_title_area, real_show_title_new, real_show_title_sex;
    private Button btn_all_area, coment_icon1, btn_very_new, coment_icon2, btn_sex, coment_icon3, edit_head_left_title;
    RealShowItem item;
    private boolean flag_coment_icon1 = true, flag_coment_icon2 = true, flag_coment_icon3 = true;
    private RealShowAdapter adapter;
    String url_select = "";
    private RealShowGridViewAdapter gridViewAdapter_area, gridViewAdapter_ribang, gridViewAdapter_sex;
    private String url = "http://lol.zhangyoubao.com/api" +
            "s/rest/UgcsService/getUserShows?order_kin" +
            "d=0&area=&page=1&sex=&i_=JfoZvarCyLfJIqs9" +
            "SRFolapk21/wEw63JVMaX7Y3v5w=&deviceId=Jfo" +
            "ZvarCyLfJIqs9SRFolapk21/wEw63JVMaX7Y3v5w=" +
            "&t_=1473768384318&p_=15299&v_=400901&a_=l" +
            "ol&pkg_=com.anzogame.lol&d_=android&osv_=" +
            "19&cha=360Martket&u_=15863305&modle_=MX4&" +
            "api=UgcsService.getUserShows&time=1473768" +
            "384&game=lol&secretId=AKIDz8krbsJ5yKBZQpn" +
            "74WFkmLPx3gnPhESA&secretVersion=v1.0&nonce" +
            "=418011&secretSignature=sqsR2tBLD2Tqvzw/Y" +
            "7IeDGR5OQU= ";
    private String url_up = "http://lol.zhangyoubao.com/apis/re" +
            "st/UgcsService/getCompanions?userid=&i_=Lf2he" +
            "/rdpOPtR1PwqdUiuwS64wm6xMV8rBpebf4NEuI=&devic" +
            "eId=Lf2he/rdpOPtR1PwqdUiuwS64wm6xMV8rBpebf4NE" +
            "uI=&t_=1473993998110&p_=26675&v_=400902&a_=lo" +
            "l&pkg_=com.anzogame.lol&d_=android&osv_=17&ch" +
            "a=baiduMartket&u_=&modle_=vivo%20Y15T&api=Ugc" +
            "sService.getCompanions&time=1473993862&game=l" +
            "ol&secretId=AKIDz8krbsJ5yKBZQpn74WFkmLPx3gnPh" +
            "ESA&secretVersion=v1.0&nonce=633005&secretSign" +
            "ature=lP9A/Jokr/S3ippTs6ORW8nJS7c=";
    private String[] server_name = new String[]{"全部大区   ", "艾欧尼亚   ", "祖安   ", "诺克萨斯   ", "班德尔城   ", "皮尔特沃夫   "
            , "战争学院   ", "巨神峰   ", "雷瑟守备   ", "裁决之地   ", "黑色玫瑰   ", "暗影岛   ", "钢铁烈阳   "
            , "均衡教派   ", "水晶之痕   ", "影流   ", "守望之海   ", "征服之海   ", "卡拉曼达   "
            , "皮城警备   ", "比尔吉沃特   ", "德玛西亚   ", "弗雷尔卓德   ", "无畏先锋   ", "恕瑞玛   "
            , "扭曲丛林   ", "巨龙之巢   ", "教育网专区   "};
    private String[] very_new = new String[]{"最新", "日榜", "周榜", "总榜"};
    private String[] sex = new String[]{"全部", "男", "女"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_real_show);
        initView();
        initData();
    }

    private void initData() {
        btn_all_area.setOnClickListener(this);
        coment_icon1.setOnClickListener(this);
        btn_very_new.setOnClickListener(this);
        coment_icon2.setOnClickListener(this);
        btn_sex.setOnClickListener(this);
        coment_icon3.setOnClickListener(this);
        edit_head_left_title.setOnClickListener(this);
        gridViewAdapter_area = new RealShowGridViewAdapter(RealShowActivity.this, server_name,1);
        gridViewAdapter_ribang = new RealShowGridViewAdapter(RealShowActivity.this, very_new,2);
        gridViewAdapter_sex = new RealShowGridViewAdapter(RealShowActivity.this, sex,3);
        gridViewAdapter_area.setOnRelectGetUrl(this);
        gridViewAdapter_ribang.setOnRelectGetUrl(this);
        gridViewAdapter_sex.setOnRelectGetUrl(this);
        real_show_title_area.setAdapter(gridViewAdapter_area);
        real_show_title_new.setAdapter(gridViewAdapter_ribang);
        real_show_title_sex.setAdapter(gridViewAdapter_sex);
        getDataFromWeb();
    }

    private void initView() {
        btn_all_area = (Button) findViewById(R.id.btn_all_area);
        coment_icon1 = (Button) findViewById(R.id.coment_icon1);
        btn_very_new = (Button) findViewById(R.id.very_new);
        coment_icon2 = (Button) findViewById(R.id.coment_icon2);
        btn_sex = (Button) findViewById(R.id.btn_sex);
        coment_icon3 = (Button) findViewById(R.id.coment_icon3);
        real_show_title_area = (GridView) findViewById(R.id.real_show_title_area);
        real_show_title_new = (GridView) findViewById(R.id.real_show_title_new);
        real_show_title_sex = (GridView) findViewById(R.id.real_show_title_sex);
        edit_head_left_title = (Button) findViewById(R.id.edit_head_left_title);
        listView = (ListView) findViewById(R.id.listView);

    }

    public void getDataFromWeb() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(url)
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Request request, IOException e) {
                        Toast.makeText(RealShowActivity.this, "数据获取失败，真抱歉，嘿嘿...", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Response response) throws IOException {
                        String result = response.body().string();
                        Gson gson = new Gson();
                        item = gson.fromJson(result, RealShowItem.class);
                        if (item.getData().size() > 0) {
                            handler.sendEmptyMessage(1);
                        } else {
                            handler.sendEmptyMessage(0);
                        }
                    }
                });

            }
        }).start();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                Toast.makeText(RealShowActivity.this, "获取数据失败", Toast.LENGTH_SHORT).show();
            } else {
                listView.setAdapter(new RealShowAdapter(RealShowActivity.this, item));
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        String item_up_url="http://lol.zhangyoubao.com/apis/rest/UgcsService/userShowDegree?id="+item.getData().get(position).getId()+"&userid=15863305&i_=JfoZvarCyLfJIqs9SRFolapk21/wEw63JVMaX7Y3v5w=&deviceId=JfoZvarCyLfJIqs9SRFolapk21/wEw63JVMaX7Y3v5w=&t_=1473824595382&p_=18491&v_=400901&a_=lol&pkg_=com.anzogame.lol&d_=android&osv_=19&cha=360Martket&u_=15863305&modle_=MX4&api=UgcsService.userShowDegree&time=1473824595&game=lol&secretId=AKIDz8krbsJ5yKBZQpn74WFkmLPx3gnPhESA&secretVersion=v1.0&nonce=438025&secretSignature=jX2LJ0lsSSz8HjD5QWuCvZ8UKYU=";
                        String item_down_url="http://lol.zhangyoubao.com/apis/rest/UgcsService/userShowComments?token=1285c08b612411d0bfc35f213eb5d92a73&page=1&id="+item.getData().get(position).getId()+"&userid=15863305&i_=JfoZvarCyLfJIqs9SRFolapk21/wEw63JVMaX7Y3v5w=&deviceId=JfoZvarCyLfJIqs9SRFolapk21/wEw63JVMaX7Y3v5w=&t_=1473824596218&p_=20999&v_=400901&a_=lol&pkg_=com.anzogame.lol&d_=android&osv_=19&cha=360Martket&u_=15863305&modle_=MX4&api=UgcsService.userShowComments&time=1473824596&game=lol&secretId=AKIDz8krbsJ5yKBZQpn74WFkmLPx3gnPhESA&secretVersion=v1.0&nonce=857403&secretSignature=Fx0haUVaZKE1Gq8dyNgKgj1JiOE=";
                        Intent item_real_show_intent=new Intent(RealShowActivity.this,ItemRealShowActivity.class);
                        Bundle bundle=new Bundle();
                        bundle.putString("item_up_url",item_up_url);
                        bundle.putString("item_down_url",item_down_url);
                        item_real_show_intent.putExtras(bundle);
                        startActivity(item_real_show_intent);
                    }
                });
            }
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.edit_head_left_title:
                finish();
                break;
            case R.id.btn_all_area:
            case R.id.coment_icon1:
                /**
                 * if前的代码块是重置另外两个选项卡，一下每个case中的前几行，功能类似
                 */
                btn_very_new.setTextColor(Color.parseColor("#afb9cf"));
                coment_icon2.setBackgroundResource(R.mipmap.community_arrow_down_d);
                real_show_title_new.setVisibility(View.INVISIBLE);
                btn_sex.setTextColor(Color.parseColor("#afb9cf"));
                coment_icon3.setBackgroundResource(R.mipmap.community_arrow_down_d);
                real_show_title_sex.setVisibility(View.INVISIBLE);
                if (flag_coment_icon1) {
                    btn_all_area.setTextColor(Color.parseColor("#318aff"));
                    coment_icon1.setBackgroundResource(R.mipmap.community_arrow_down_p);
                    flag_coment_icon1 = false;
                    real_show_title_area.setVisibility(View.VISIBLE);
                } else {
                    btn_all_area.setTextColor(Color.parseColor("#afb9cf"));
                    coment_icon1.setBackgroundResource(R.mipmap.community_arrow_down_d);
                    real_show_title_area.setVisibility(View.INVISIBLE);
                    flag_coment_icon1 = true;
                }
                break;
            case R.id.very_new:
            case R.id.coment_icon2:
                btn_all_area.setTextColor(Color.parseColor("#afb9cf"));
                coment_icon1.setBackgroundResource(R.mipmap.community_arrow_down_d);
                real_show_title_area.setVisibility(View.INVISIBLE);
                btn_sex.setTextColor(Color.parseColor("#afb9cf"));
                coment_icon3.setBackgroundResource(R.mipmap.community_arrow_down_d);
                real_show_title_sex.setVisibility(View.INVISIBLE);
                flag_coment_icon3 = true;
                if (flag_coment_icon2) {
                    btn_very_new.setTextColor(Color.parseColor("#318aff"));
                    coment_icon2.setBackgroundResource(R.mipmap.community_arrow_down_p);
                    flag_coment_icon2 = false;
                    real_show_title_new.setVisibility(View.VISIBLE);
                } else {
                    btn_very_new.setTextColor(Color.parseColor("#afb9cf"));
                    coment_icon2.setBackgroundResource(R.mipmap.community_arrow_down_d);
                    real_show_title_new.setVisibility(View.INVISIBLE);
                    flag_coment_icon2 = true;
                }
                break;
            case R.id.btn_sex:
            case R.id.coment_icon3:
                btn_all_area.setTextColor(Color.parseColor("#afb9cf"));
                coment_icon1.setBackgroundResource(R.mipmap.community_arrow_down_d);
                real_show_title_area.setVisibility(View.INVISIBLE);
                btn_very_new.setTextColor(Color.parseColor("#afb9cf"));
                coment_icon2.setBackgroundResource(R.mipmap.community_arrow_down_d);
                real_show_title_new.setVisibility(View.INVISIBLE);
                if (flag_coment_icon3) {
                    btn_sex.setTextColor(Color.parseColor("#318aff"));
                    coment_icon3.setBackgroundResource(R.mipmap.community_arrow_down_p);
                    flag_coment_icon3 = false;
                    real_show_title_sex.setVisibility(View.VISIBLE);
                } else {
                    btn_sex.setTextColor(Color.parseColor("#afb9cf"));
                    coment_icon3.setBackgroundResource(R.mipmap.community_arrow_down_d);
                    real_show_title_sex.setVisibility(View.INVISIBLE);
                    flag_coment_icon3 = true;
                }
                break;
        }
    }

    @Override
    public void relectGetUrl(int type, String parmas) {

        switch (type) {
            case 1:
                String path1 = "http://lol.zhangyoubao.com/apis/rest/UgcsService/getUserShows?order_kind=0&area="+parmas+"&page=1&sex=&i_=JfoZvarCyLfJIqs9SRFolapk21/wEw63JVMaX7Y3v5w=&deviceId=JfoZvarCyLfJIqs9SRFolapk21/wEw63JVMaX7Y3v5w=&t_=1473837010057&p_=2516&v_=400901&a_=lol&pkg_=com.anzogame.lol&d_=android&osv_=19&cha=360Martket&u_=15863305&modle_=MX4&api=UgcsService.getUserShows&time=1473837010&game=lol&secretId=AKIDz8krbsJ5yKBZQpn74WFkmLPx3gnPhESA&secretVersion=v1.0&nonce=971863&secretSignature=6YHX3ks70/9Ho7qI5OhQx/ctV5M=";
                url_select = path1;
                break;
            case 2:
                String path2 = "http://lol.zhangyoubao.com/apis/rest/UgcsService/getUserShows?order_kind="+parmas+"&area=&page=1&sex=&i_=JfoZvarCyLfJIqs9SRFolapk21/wEw63JVMaX7Y3v5w=&deviceId=JfoZvarCyLfJIqs9SRFolapk21/wEw63JVMaX7Y3v5w=&t_=1473837058474&p_=27767&v_=400901&a_=lol&pkg_=com.anzogame.lol&d_=android&osv_=19&cha=360Martket&u_=15863305&modle_=MX4&api=UgcsService.getUserShows&time=1473837058&game=lol&secretId=AKIDz8krbsJ5yKBZQpn74WFkmLPx3gnPhESA&secretVersion=v1.0&nonce=811652&secretSignature=UnFWP9XxRxONrVzsF33Wq5vVSrQ= ";
                url_select = path2;
                break;
            case 3:
                String path3 = "http://lol.zhangyoubao.com/apis/rest/UgcsService/getUserShows?order_kind=0&area=&page=1&sex="+parmas+"&i_=JfoZvarCyLfJIqs9SRFolapk21/wEw63JVMaX7Y3v5w=&deviceId=JfoZvarCyLfJIqs9SRFolapk21/wEw63JVMaX7Y3v5w=&t_=1473837145375&p_=18470&v_=400901&a_=lol&pkg_=com.anzogame.lol&d_=android&osv_=19&cha=360Martket&u_=15863305&modle_=MX4&api=UgcsService.getUserShows&time=1473837145&game=lol&secretId=AKIDz8krbsJ5yKBZQpn74WFkmLPx3gnPhESA&secretVersion=v1.0&nonce=525601&secretSignature=1xqNaMPPvB+w0+XIUEBcrbxI9BY=";
                url_select = path3;
                break;
            default:
                return;
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(url_select)
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Request request, IOException e) {
                        Toast.makeText(RealShowActivity.this, "数据获取失败，真抱歉，嘿嘿...", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Response response) throws IOException {
                        String result = response.body().string();
                        Gson gson = new Gson();
                        item = gson.fromJson(result, RealShowItem.class);
                        if (item.getData().size() > 0) {
                            handler.sendEmptyMessage(1);
                        } else {
                            handler.sendEmptyMessage(0);
                        }
                    }
                });
            }
        }).start();
    }
}
